﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 虚拟方法
{
    class Program
    {
        static void Main(string[] args)
        {
            Account acc1 = new Account("0001", "123456", 10000);
            BankCard[] cards = new BankCard[]
            {
                new BankCard("A001","1357",2000),
                new CreditCard ("A002","2468"),
                new CreditCard("A003","")
            };
            foreach(BankCard c1 in cards)
            {
                c1.pay(acc1, 3000);
                c1.Query();
            }
            Console.ReadKey();
        }
    }
    public class Account
    {
        protected string id;
        protected string password;
        protected decimal money;

        public decimal Money
        {
            get { return money; }
        }

        public Account(string id,string password,decimal money=0)
        {
            this.id = id;
            this.password = password;
            this.money = money;
        }

        public void Deposit(decimal x)
        {
            money += x;
        }

        public bool Withdraw(decimal x)
        {
            if (money < x)
                return false;
            money -= x;
            return true;
        }

        public virtual void Query()
        {
            Console.WriteLine("账号{0}:余额{1}",id,money );
        }
    }
    public class BankCard:Account
    {
        public BankCard (string id,string password,decimal money=0)
            :base (id,password ,money )
        { }

        public virtual bool pay(Account acc1,decimal price)
        {
            if(!this .Withdraw (price ))
            {
                Console.WriteLine("余额不足，支付失败");
                return false;
            }
            acc1.Deposit(price);
            Console.WriteLine("卡{0}消费{1}",id,price);
            return true;
        }
    }
    public class CreditCard:BankCard
    {
        private decimal limit;

        public CreditCard (string id,string password,decimal limit=5000)
            :base (id,password ,0)
        {
            this.limit = limit;
        }

        public override void Query()
        {
            Console.WriteLine("卡号{0}:剩余额度{1}",id,limit +=money );
        }

        public override bool pay(Account acc1, decimal price)
        {
            if(price -money >limit)
            {
                Console.WriteLine("额度不足");
                return false;
            }
            money -= price;
            acc1.Deposit(price);
            Console.WriteLine("卡{0}消费{1}",id,price);
            return true;
        }
    }
}
